/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.widget;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.util.ImageUtils;
import com.mol.market.common.util.Utils;
import com.mol.market.common.vo.RecommendTopic;
import com.mol.market.common.widget.SlowGallery.TopRecommendAdapter;
import com.mol.market.ui.InstallNecessaryActivity;

/**
 * @author andrew.wang
 * @date 2011-12-28
 * @since Version 0.9
 */
public class TopGallery implements AnimationListener, OnItemClickListener,
        OnItemSelectedListener {

    private Activity mContext;
    private RelativeLayout mHomeGallery;
    private ArrayList<HashMap<String, Object>> mDataSource;
    // private ScreenShotGallery mGallery;
    private SlowGallery mGallery;
    private ImageView mMstaerAvatar;
    private IndicatorDrawable mIndicator;
    private TextView mTitle;
    private int mCurrentPage;

    public TopGallery(Activity ctx, ViewGroup parent, ArrayList<HashMap<String, Object>> data) {
        mContext = ctx;
        mDataSource = data;
        mHomeGallery = (RelativeLayout) LayoutInflater.from(ctx).inflate(
                R.layout.home_gallery_layout, parent, false);
        mHomeGallery.setFocusableInTouchMode(true);

        mGallery = (SlowGallery) mHomeGallery.findViewById(R.id.home_gallery);
        mGallery.setOnItemSelectedListener(this);
        mGallery.setOnItemClickListener(this);
        mTitle = (TextView) mHomeGallery.findViewById(R.id.home_gallery_title);
        mMstaerAvatar = (ImageView) mHomeGallery.findViewById(R.id.home_gallery_user);
        mIndicator = (IndicatorDrawable) mHomeGallery.findViewById(R.id.home_incidator);
        mIndicator.setPageNumber(data.size());
        initAnimation();
        initImage();
        updateAvatar(mCurrentPage);
        updateTitle();
    }

    public View getView() {
        return mHomeGallery;
    }

    private void initImage() {
        TopRecommendAdapter adapter = new TopRecommendAdapter(mContext, mDataSource);
        mGallery.setAdapter(adapter);
    }

    private static AnimationSet mAvatarInAnimation;
    private static AnimationSet mAvatarOutAnimation;

    private void initAnimation() {

        final long DURATION = 500;

        Animation mAvatarFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
        mAvatarFadeInAnimation.setDuration(DURATION);
        mAvatarFadeInAnimation.setFillAfter(true);
        mAvatarFadeInAnimation.setAnimationListener(this);

        Animation mAvatarFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
        mAvatarFadeOutAnimation.setDuration(DURATION);
        mAvatarFadeOutAnimation.setFillAfter(true);
        mAvatarFadeOutAnimation.setAnimationListener(this);

        Animation mInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
        mInAnimation.setDuration(DURATION);
        mInAnimation.setFillAfter(true);

        Animation mOutAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f);
        mOutAnimation.setDuration(DURATION);
        mOutAnimation.setFillAfter(true);

        mAvatarInAnimation = new AnimationSet(false);
        mAvatarInAnimation.addAnimation(mAvatarFadeInAnimation);
        mAvatarInAnimation.addAnimation(mInAnimation);
        mAvatarInAnimation.setAnimationListener(this);

        mAvatarOutAnimation = new AnimationSet(false);
        mAvatarOutAnimation.addAnimation(mAvatarFadeOutAnimation);
        mAvatarOutAnimation.addAnimation(mOutAnimation);
        mAvatarOutAnimation.setAnimationListener(this);
    }

    private void updateTitle() {

        HashMap<String, Object> item = mDataSource.get(mCurrentPage);
        String title = (String) item.get(Constants.KEY_RECOMMEND_NAME);
        mTitle.setText(title);
    }

    private void updateAvatar(int prePage) {
        HashMap<String, Object> item = mDataSource.get(prePage);

        Object avatar = (Object) item.get(Constants.KEY_ICON_URL);
        if (avatar instanceof String) {
            ImageUtils.download(mContext, (String) avatar, mMstaerAvatar,
                    R.drawable.default_user_home, true, false);
        } else if (avatar instanceof Integer) {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inPurgeable = true;
            o.inInputShareable = true;
            Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), (Integer) avatar, o);
            mMstaerAvatar.setImageBitmap(ImageUtils.createHomeUserIcon(mContext, bmp));
        }
    }

    private boolean mIsInAnimationEnd;
    private int mNextPage;

    @Override
    public void onAnimationEnd(Animation paramAnimation) {

        if (!mIsInAnimationEnd) {
            return;
        }

        mMstaerAvatar.startAnimation(mAvatarInAnimation);
        if (mIsInAnimationEnd) {
            updateAvatar(mNextPage);
            mIsInAnimationEnd = false;
        }
    }

    @Override
    public void onAnimationRepeat(Animation paramAnimation) {
        // do nothing
    }

    @Override
    public void onAnimationStart(Animation paramAnimation) {
        // do nothing
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        mIndicator.setPage(arg2);
        mCurrentPage = arg2;
        updateTitle();
        mNextPage = arg2;
        mIsInAnimationEnd = true;
        mMstaerAvatar.startAnimation(mAvatarOutAnimation);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        
        HashMap<String, Object> item = mDataSource.get(arg2);

        Object id = item.get(Constants.KEY_ID);
        if (id == null) {
            // 装机必备专题
            Intent i = new Intent(mContext, InstallNecessaryActivity.class);
            mContext.startActivity(i);
            Utils.trackEvent(mContext, Constants.GROUP_2, Constants.EVENT_CLICK_MUST_INSTALL);
            
        } else {
            RecommendTopic topic = Utils.mapToTopic(item);
            Utils.gotoMaster(mContext, topic);
            Utils.trackEvent(mContext, Constants.GROUP_2, 
                    String.format(Constants.EVENT_CLICK_TOPIC, topic.title));
        }
    }

}
